// ActionScript file
package com.sundhage.gui {
	import flash.display.*;
	import flash.events.*;
	import com.sundhage.util.JSXMLHelper;
	
	public class JSCheckbox extends Sprite {
		
		private var settings:JSCheckboxSettings;
		private var assets:JSCheckboxAssets;
		public static var CHANGE:String = "checkboxChange";
		public var selectedBoxes:Array = new Array();
		
		
		protected var buttons:Array = new Array();		// alla knappar
		
		private var content:Sprite;
		private var bkg:Sprite;
		
		
		public function setSelected(id:int):void {
			buttons[id].select();
			
		}
		
		public function setUnSelected(id:int):void {
			buttons[id].unselect();
			
		}
		private function swapBtn(id:int):void {
			buttons[id].isSelected = !buttons[id].isSelected;
			if (buttons[id].isSelected) buttons[id].select();
			else buttons[id].unselect();
		}
		
		
		private function itemSelected(e:Event):void {
			swapBtn(e.target.id);
			dispatchEvent(new Event(CHANGE));
		}
		
		public function addItem(txt:String, objData:Object):int {
			var nId:int = buttons.length;
			
			var nItem:Item = new Item(settings, assets, txt, nId, objData);
			nItem.addEventListener(Item.SELECTED, itemSelected);
			buttons.push(nItem);
			return nId;
		}
		
		public function render():void {
			var tx:int = settings.generalPad;
			var ty:int = settings.generalPad;
			for (var i:int = 0; i<buttons.length; i++) {
				buttons[i].x = tx;
				buttons[i].y = ty;
				if (settings.order == "horiz") tx += buttons[i].width+settings.buttonPad;
				else ty += buttons[i].height+settings.buttonPad;
				addChild(buttons[i]);
			}
			if (assets.hasBackground()) {
				var bmpBkg:BitmapData = assets.getBackground();
				var w:int = this.width;
				var h:int = this.height;
				bkg.graphics.clear();
				bkg.graphics.beginBitmapFill(bmpBkg, null, true, true);
				bkg.graphics.drawRoundRect(0,0,w+settings.generalPad*2, h+settings.generalPad*2, settings.curve, settings.curve);
				bkg.graphics.endFill();
			}
		}

		public function getSelectedItems():Array {
			var a:Array = new Array();
			for (var i:int = 0; i<buttons.length; i++) {
				if (buttons[i].isSelected) {
					var o:Object = new Object();
					o.id = i;
					o.data = buttons[i].objData;
					a.push(o);
				}
			}
			return a;
		}
		
		public function getXML():String {
			var s:String = "";
			s += "<JSCheckbox id=\""+settings.xmlId+"\">";
			for (var i:int = 0; i<buttons.length; i++) {
				s += "<item selected=\""+buttons[i].isSelected+"\" id=\""+i+"\">";
				s += "<data>";
				s += JSXMLHelper.OBJ2XML(buttons[i].objData);
				s += "</data>";
				s += "</item>";
			}
			s += "</JSCheckbox>";
			return s;
			
		}
		public function JSCheckbox(settings:JSCheckboxSettings, assets:JSCheckboxAssets) {
			this.settings = settings;
			this.assets = assets;
			bkg = new Sprite();
			addChild(bkg);
			content = new Sprite();
			addChild(content);

			
		}

	}
	
	
}

// imports for Item

import com.sundhage.gui.*;
import flash.display.*;
import flash.events.*;

class Item extends Sprite {
	
	public static var SELECTED:String = "itemSelected";
	
	private var settings:JSCheckboxSettings;
	private var assets:JSCheckboxAssets;
	private var sprTxt:JSText;
	
	private var selected:Sprite;
	private var unselected:Sprite;
	
	private var txt:String = "";
	public var isSelected:Boolean = false;
	public var id:int;
	public var objData:Object;
	public var data:Object = new Object();			// fri att använda

	public var doFixXY:Boolean = true;


	public function getTxt():String {
		return txt;
	}
	
	public function getTextHeight():int {
		return sprTxt.height;
	}
	public function getTextWidth():int {
		return sprTxt.width;
	}
	public function getBtnWidth():int {
		if (selected.width > unselected.width)
			return selected.width
		else
			return unselected.width;
	}
	public function getBtnHeight():int {
		if (selected.height > unselected.height)
			return selected.height;
		else
			return unselected.height;
	}
	public function moveText(x:int, y:int):void {
		sprTxt.x = x;
		sprTxt.y = y;
	}
	public function moveButtons(x:int, y:int):void {
		selected.x = x;
		selected.y = y;
		unselected.x = x;
		unselected.y = y;
	}
	
	public function select():void {
		isSelected = true;
		selected.visible = true;
		unselected.visible = false;
		if (doFixXY) fixXY(sprTxt, selected);
	}
	
	public function unselect():void {
		isSelected = false;
		selected.visible = false;
		unselected.visible = true;
		if (doFixXY) fixXY(sprTxt, unselected);
	}
	
	
	private function fixXY(t:JSText, b:*):void {
		//trace(t + " " + b);
		t.x = b.x+b.width+settings.generalPad;
		
		if (t.height > b.height) {
			b.y = (t.getHeight())/2-b.height/2;
		} else {
			// anpassa t
			t.y = (b.y+b.height)/2-t.getHeight()/2;
		}
		
	}
	public function mClick(e:Event):void {
		dispatchEvent(new Event(SELECTED));
	}
	
	public function Item(settings:JSCheckboxSettings, assets:JSCheckboxAssets, txt:String, id:int, objData:Object) {
		this.settings = settings;
		this.assets = assets;
		this.id = id;
		this.objData = objData;
		this.txt = txt;
		
		this.selected = assets.getSelected();
		this.unselected = assets.getUnselected();
		
		unselected.buttonMode = true;
		selected.buttonMode = true;
		addChild(selected);
		addChild(unselected);
		
		
		unselected.addEventListener(MouseEvent.CLICK, mClick);
		selected.addEventListener(MouseEvent.CLICK, mClick);
		
		sprTxt = new JSText(txt, settings.txtFormat, settings.txtColor, true);
		//trace("vafan" + sprTxt);
		fixXY(sprTxt, unselected);
		unselect();
		addChild(sprTxt);
		//var txt:JSText = new JSText(settings.title, settings.headTextFormat, settings.headTextColor);
	}
}
